package com.xxx;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * Hello world!
 *
 */
public class App {
//    static ThreadLocal<Integer> threadLocal = new InheritableThreadLocal<Integer>();

    public static void main(String[] args) {
//        threadLocal.set(new Integer(123));
//
//        ExecutorService fixPool = Executors.newFixedThreadPool(10);
//        for (int i = 0; i < 20; i++) {
//            final int index = i;
//            try {
//                Thread.sleep(index * 500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//            fixPool.execute(new Runnable() {
//                public void run() {
//                    System.out.println("Thread = " + Thread.currentThread().getName() + ", SonThread = " + threadLocal.get());
//                }
//            });
//        }
//        fixPool.shutdown();

//        People p1 = new People("Jack", 12);
//        System.out.println(p1.hashCode());
//
//        HashMap<People, Integer> hashMap = new HashMap<People, Integer>();
//        hashMap.put(p1, 1);
//
//        p1.setAge(13);

//        System.out.println(hashMap.get(p1));

        int port = 8080;

        EventLoopGroup bossGroup = new NioEventLoopGroup(2);
        EventLoopGroup workerGroup = new NioEventLoopGroup(4);

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();

                            // 以("\n")为结尾分割的 解码器
                            pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));

                            // 字符串解码 和 编码
                            pipeline.addLast("decoder", new StringDecoder());
                            pipeline.addLast("encoder", new StringEncoder());

                            // 自己的逻辑Handler
                            pipeline.addLast("handler", new TimeServerHandler());
                        }
                    });

            ChannelFuture f = b.bind(port).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
